﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InventoryProject.Framework.Entity;
using InventoryProject.FrameWork;
using FrameWork.FrameWork.Utility;
using System.Data;

namespace InventoryProject.User
{
    public partial class UserManagement : System.Web.UI.Page
    {
        UserEntity _entity;
        UserAccess _access;
        string LoginID;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginID = "admin"; // = Session["LogID"]

            this.ClientScript.GetPostBackEventReference(this, string.Empty);
            if (!IsPostBack)
            {

                LoadData("");
            }
            else
            {
                if ((Request.Form["__EventTarget"] != null && Request.Form["__EventTarget"] == "myDblClick"))
                {
                    grdData.SelectedIndex = int.Parse(Request.Form["__EVENTARGUMENT"].ToString());
                    GridViewRow dr;
                    dr = grdData.SelectedRow;
                    txtUserID.Text = dr.Cells[0].Text;
                    txtUserNm.Text = dr.Cells[1].Text;
                    txtDisplayNm.Text = HttpUtility.HtmlDecode(dr.Cells[2].Text);
                    txtPassword.Text = "";
                    txtRePassword.Text = "";
                    //Response.Write(Request.Form["__EVENTARGUMENT"].ToString());
                }
            }
        }

        private void LoadData(string UserID)
        {
            _access = new UserAccess();
            DataTable dtData = _access.UserList_Qry(UserID);
            grdData.DataSource = dtData;
            grdData.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string SType = "";
            _access = new UserAccess();
            DataTable dtRes;
            _entity = new UserEntity();
            _entity.UserID = txtUserID.Text;
            _entity.Username = txtUserNm.Text;
            _entity.DisplayName = txtDisplayNm.Text;
            _entity.Password = txtPassword.Text;

            if (txtUserID.Text.Trim() == "")
                SType = "A";
            else
                SType = "U";

            dtRes = _access.UserInfo_Save(SType, _entity, LoginID);            

            MessageHelper.ShowMessage(this, dtRes.Rows[0][1].ToString());
            txtUserID.Text = dtRes.Rows[0][2].ToString();

            LoadData("");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            ClearData();
        }

        protected void grdData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmd = e.CommandName;
            if (cmd == "Delete")
            {
                

               
            }
        }

        private void ClearData()
        { 
            txtUserID.Text = "";
            txtUserNm.Text = "";
            txtDisplayNm.Text = "";
            txtPassword.Text = "";
            txtRePassword.Text = "";
        }

      

        protected void grdData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            _access = new UserAccess();
            _entity = new UserEntity();
            _entity.UserID = e.Values[0].ToString();
            _access.UserInfo_Save("D", _entity, LoginID);

            LoadData("");
            ClearData();
        }

        protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.color='red'";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='black';";
                e.Row.Attributes.Add("onclick", "Javascript:__doPostBack('myDblClick','" + e.Row.RowIndex + "');");
            }
        }

      
    }
}